//
//  ViewController.swift
//  H264_VedioToolbox
//
//  Created by meitianhui2 on 2018/1/3.
//  Copyright © 2018年 DeLongYang. All rights reserved.
//

import UIKit
import AVFoundation
import VideoToolbox


class ViewController: UIViewController,AVCaptureVideoDataOutputSampleBufferDelegate {
    
    // 在OC中 VTCompressionSessionRef  renamed to  VTCompressionSession
    var _encodeSession:VTCompressionSession?
    var _encodeQueue:DispatchQueue = DispatchQueue.init(label: "encodeQueue")
    var _frameCount:CLong?
    var _h264File:FILE?
    var _spsppsFound:Int?
    var documentDictionary:String?
    var videoCaptureSession:AVCaptureSession = AVCaptureSession()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    // MARK: 初始化
    func initVedioCapture()  {
        self.videoCaptureSession = AVCaptureSession()
        // 使用了新的 vga640x480
        self.videoCaptureSession.sessionPreset = AVCaptureSession.Preset.vga640x480
        let device:AVCaptureDevice? = AVCaptureDevice.default(for: .video)
        if device == nil {
            print("No Video device found")
        }
        
        // 这里要判断一下
        let inputDevice:AVCaptureDeviceInput = try! AVCaptureDeviceInput.init(device: device!)
        if self.videoCaptureSession .canAddInput(inputDevice) {
           print("add video input to video session \(inputDevice)")
           self.videoCaptureSession .addInput(inputDevice)
        }
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    
    // MARK: videotoolbox method
    func startEncodeSession(width:Int,height:Int,framerate:Int, fpsbitrate:Int) -> Int {
        var status:OSStatus
        _frameCount = 0
       
        // 在swift 中 调用C 函数有问题
//        let cb : @convention(c) (Void,Void,OSStatus,VTEncodeInfoFlags,CMSampleBuffer)->Void = {
//
//        }
        
        //
//        status = VTCompressionSessionCreate(kCFAllocatorDefault, Int32(width), Int32(height), kCMVideoCodecType_H264, NULL as! CFDictionary, NULL as! CFDictionary, NULL as? CFAllocator, cb, self, &_encodeSession);
        
        return -1
    }
    
  

}






































